<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="nav-bar">
				<view class="nav-left" @click="goBack">
					<text class="back-icon">‹</text>
				</view>
				<text class="nav-title">修改手机号</text>
				<view class="nav-right">
					<text class="more-icon">⋯</text>
					<text class="circle-icon">○</text>
				</view>
			</view>
		</view>

		<!-- 修改手机号表单 -->
		<view class="form-container">
			<view class="form-title">
				<text class="title-text">修改手机号</text>
				<text class="subtitle-text">请输入新的手机号码</text>
			</view>

			<view class="form-content">
				<!-- 当前手机号 -->
				<view class="form-item">
					<text class="form-label">当前手机号</text>
					<view class="current-phone">
						<text class="phone-text">{{ currentPhone }}</text>
					</view>
				</view>

				<!-- 新手机号输入 -->
				<view class="form-item">
					<text class="form-label">新手机号</text>
					<view class="input-container">
						<input 
							class="phone-input" 
							type="number" 
							v-model="newPhone" 
							placeholder="请输入新手机号"
							maxlength="11"
							@input="onPhoneInput"
						/>
					</view>
				</view>

				<!-- 验证码输入 -->
				<view class="form-item">
					<text class="form-label">验证码</text>
					<view class="code-container">
						<input 
							class="code-input" 
							type="number" 
							v-model="verifyCode" 
							placeholder="请输入验证码"
							maxlength="6"
						/>
						<button 
							class="send-code-btn" 
							:class="{ disabled: !canSendCode }"
							:disabled="!canSendCode"
							@click="sendVerifyCode"
						>
							{{ codeButtonText }}
						</button>
					</view>
				</view>
			</view>

			<!-- 确认修改按钮 -->
			<view class="submit-section">
				<button 
					class="submit-btn" 
					:class="{ disabled: !canSubmit }"
					:disabled="!canSubmit"
					@click="submitChange"
				>
					确认修改
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentPhone: '15023457896', // 当前手机号
				newPhone: '', // 新手机号
				verifyCode: '', // 验证码
				countdown: 0, // 倒计时
				canSendCode: true, // 是否可以发送验证码
				codeButtonText: '发送验证码' // 验证码按钮文字
			}
		},
		computed: {
			// 是否可以提交
			canSubmit() {
				return this.newPhone.length === 11 && 
					   this.verifyCode.length === 6 && 
					   this.isValidPhone(this.newPhone);
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			// 手机号输入处理
			onPhoneInput(e) {
				this.newPhone = e.detail.value;
			},
			// 验证手机号格式
			isValidPhone(phone) {
				const phoneReg = /^1[3-9]\d{9}$/;
				return phoneReg.test(phone);
			},
			// 发送验证码
			sendVerifyCode() {
				if (!this.isValidPhone(this.newPhone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}

				if (this.newPhone === this.currentPhone) {
					uni.showToast({
						title: '新手机号不能与当前手机号相同',
						icon: 'none'
					});
					return;
				}

				// 开始倒计时
				this.startCountdown();

				// 模拟发送验证码
				uni.showLoading({
					title: '发送中...'
				});

				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '验证码已发送',
						icon: 'success'
					});
					
					// 模拟验证码（实际项目中应该从服务器获取）
					this.verifyCode = '123456';
				}, 1500);
			},
			// 开始倒计时
			startCountdown() {
				this.countdown = 60;
				this.canSendCode = false;
				this.codeButtonText = `${this.countdown}s后重发`;

				const timer = setInterval(() => {
					this.countdown--;
					this.codeButtonText = `${this.countdown}s后重发`;

					if (this.countdown <= 0) {
						clearInterval(timer);
						this.canSendCode = true;
						this.codeButtonText = '发送验证码';
					}
				}, 1000);
			},
			// 提交修改
			submitChange() {
				if (!this.canSubmit) {
					uni.showToast({
						title: '请完善信息',
						icon: 'none'
					});
					return;
				}

				// 验证验证码（实际项目中应该验证服务器返回的验证码）
				if (this.verifyCode !== '123456') {
					uni.showToast({
						title: '验证码错误',
						icon: 'none'
					});
					return;
				}

				uni.showLoading({
					title: '修改中...'
				});

				// 模拟修改手机号
				setTimeout(() => {
					uni.hideLoading();
					
					// 更新本地存储的用户信息
					const userInfo = uni.getStorageSync('userInfo') || {};
					userInfo.phone = this.newPhone;
					uni.setStorageSync('userInfo', userInfo);

					uni.showModal({
						title: '修改成功',
						content: `手机号已修改为：${this.newPhone}`,
						showCancel: false,
						success: () => {
							// 返回上一页
							uni.navigateBack({
								delta: 1
							});
						}
					});
				}, 2000);
			}
		},
		onLoad() {
			// 页面加载时获取当前手机号
			this.getCurrentPhone();
		},
		// 获取当前手机号
		getCurrentPhone() {
			const userInfo = uni.getStorageSync('userInfo');
			if (userInfo && userInfo.phone) {
				this.currentPhone = userInfo.phone;
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #f5f7fa;
		min-height: 100vh;
	}

	/* 顶部导航栏 */
	.header {
		background: linear-gradient(135deg, #3183FB 0%, #3183FB 100%);
		padding-top: var(--status-bar-height);
	}

	.nav-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 30rpx 30rpx;
		height: 88rpx;
	}

	.nav-left {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		font-size: 48rpx;
		color: white;
		font-weight: bold;
	}

	.nav-title {
		color: white;
		font-size: 36rpx;
		font-weight: bold;
	}

	.nav-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
		width: 60rpx;
		height: 60rpx;
		justify-content: center;
	}

	.more-icon {
		font-size: 32rpx;
		color: white;
	}

	.circle-icon {
		font-size: 24rpx;
		color: white;
	}

	/* 表单容器 */
	.form-container {
		background-color: white;
		margin: 20rpx;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	/* 表单标题 */
	.form-title {
		padding: 40rpx 30rpx 20rpx;
		text-align: center;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.title-text {
		display: block;
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
	}

	.subtitle-text {
		font-size: 26rpx;
		color: #666;
	}

	/* 表单内容 */
	.form-content {
		padding: 30rpx;
	}

	.form-item {
		margin-bottom: 40rpx;
	}

	.form-item:last-child {
		margin-bottom: 0;
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 15rpx;
		font-weight: 500;
	}

	/* 当前手机号 */
	.current-phone {
		padding: 20rpx;
		background-color: #f8f9fa;
		border-radius: 10rpx;
		border: 1rpx solid #e9ecef;
	}

	.phone-text {
		font-size: 32rpx;
		color: #333;
	}

	/* 输入容器 */
	.input-container {
		position: relative;
	}

	.phone-input {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		background-color: #f8f9fa;
		border-radius: 10rpx;
		border: 1rpx solid #e9ecef;
		font-size: 32rpx;
		color: #333;
	}

	.phone-input:focus {
		border-color: #3183FB;
		background-color: white;
	}

	/* 验证码容器 */
	.code-container {
		display: flex;
		gap: 20rpx;
		align-items: center;
	}

	.code-input {
		flex: 1;
		height: 80rpx;
		padding: 0 20rpx;
		background-color: #f8f9fa;
		border-radius: 10rpx;
		border: 1rpx solid #e9ecef;
		font-size: 32rpx;
		color: #333;
	}

	.code-input:focus {
		border-color: #3183FB;
		background-color: white;
	}

	.send-code-btn {
		width: 200rpx;
		height: 80rpx;
		background-color: #3183FB;
		color: white;
		border: none;
		border-radius: 10rpx;
		font-size: 26rpx;
		white-space: nowrap;
	}

	.send-code-btn.disabled {
		background-color: #ccc;
		color: #999;
	}

	/* 提交按钮 */
	.submit-section {
		padding: 30rpx;
		border-top: 1rpx solid #f0f0f0;
	}

	.submit-btn {
		width: 100%;
		height: 88rpx;
		background-color: #3183FB;
		color: white;
		border: none;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-weight: 500;
	}

	.submit-btn.disabled {
		background-color: #ccc;
		color: #999;
	}

	.submit-btn:active:not(.disabled) {
		background-color: #2a6fd8;
	}
</style>